package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsCanlDto;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmNewsService;
import com.itheima.wemedia.service.WmUserService;
import com.itheima.wemedia.vo.WmNewsVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApiWmUserFeignController {


    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 通过远程调用apUserId查询自媒体账号信息
     *
     * @param apUserId
     * @return
     */
    @GetMapping("/api/wmUser/getByApUserId/{apUserId}")
    public ResultVo<WmUser> getByApUserId(@PathVariable("apUserId") long apUserId) {
        WmUser wmUser = wmUserService.getByApUserId(apUserId);
        return ResultVo.ok(wmUser);
    }

    /**
     * 通过远程调用添加自媒体信息
     *
     * @param wmUser
     * @return
     */
    @PostMapping("/api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser) {
        if (wmUserService.save(wmUser)) {
            return ResultVo.ok(wmUser);
        }
        return ResultVo.error();
    }


    /**
     * 通过远程调用自媒体微服务添加素材
     *
     * @param wmMaterial
     * @return
     */

    @PostMapping("/api/wmMaterial/add")
    public ResultVo<WmMaterial> add(@RequestBody WmMaterial wmMaterial) {
        if (wmMaterialService.save(wmMaterial)) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 远程调用admin微服务查询频道列表信息
     *
     * @return
     */
    @Cacheable
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> channel() {
        List<AdChannel> channelList = wmMaterialService.channel();
        return ResultVo.ok(channelList);
    }


    /**
     * 通过远程调用weNewsId查询文章信息
     *
     * @param newsId
     * @return
     */
    @GetMapping("/api/wmNews/getByWmNewsId/{newsId}")
    public ResultVo<WmNews> getByWmNewsId(@PathVariable("newsId") long newsId) {
        WmNews wmNews = wmNewsService.getById(newsId);
        return ResultVo.ok(wmNews);
    }

    /**
     * 通过远程调用更新文章信息
     *
     * @param wmNews
     * @return
     */
    @PutMapping("/api/wmNews/update")
    public ResultVo update(@RequestBody WmNews wmNews) {

        boolean update = wmNewsService.updateById(wmNews);
        if (update) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }


    /**
     * 后台管理： 文章人工审核，分页查询
     * @param dto
     * @return
     */
    @PostMapping("/news/list_vo")
    public PageResultVo wmNewsPageAll(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNewsVo> wmNewsVo =wmNewsService.wmNewsPageAll(dto);
        return wmNewsVo;
    }


    /**
     * 后台管理： 文章人工审核，文章审核通过
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_pass")
    public ResultVo authPass(@RequestBody WmNewsCanlDto dto){
        if(wmNewsService.authPass(dto)){
           return ResultVo.ok();
        }

        return ResultVo.error();
    }


    /**
     * 后台管理： 文章人工审核，文章审核驳回
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_fail")
    public ResultVo authFail(@RequestBody WmNewsCanlDto dto){
        if(wmNewsService.authFail(dto)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 后台管理： 文章人工审核，查询文章详情
     * @param wmNewsId
     * @return
     */
    @GetMapping("/news/one_vo/{wmNewsId}")
    public ResultVo getWmNewsOne(@PathVariable("wmNewsId")  Long wmNewsId){
        WmNewsVo vo=  wmNewsService.getWmNewsOne(wmNewsId);
      return ResultVo.ok(vo);
    }


    /**
     * 远程调用自媒体微服务查询文章信息  ————文章同步
     * @param dto  ：为了做分片处理
     * @return
     */
    @PostMapping("/api/wmNews/syncArticleTask")
    public ResultVo<List<WmNewsVo>>  syncArticleTask(@RequestBody WmNewsPageReqDto dto){
        List<WmNewsVo> list=   wmNewsService.syncArticleTask(dto);
        return ResultVo.ok(list);
    }

}
